/**
 * 保存房间中的所有玩家对象
 * @author LuoDong
 * @email luo4521@online.net.tw
 */
package com.addcn.texas.data
{	
	import com.addcn.texas.inter.IUser;
	import flash.utils.Dictionary;
	public class RoomUsers
	{
		private static var _instance : RoomUsers
		private var _user_list : Object;  //房间中所有玩家列表
		private var _sit_list : Object;  //座位号对应的玩家对象		
		
		function RoomUsers() : void
		{
			if (_instance != null) {
				trace("[RoomUsers] 为单例类!");
			}else {
				_initialize();
			}
		}
		
		public static function getInstance() : RoomUsers
		{
			if (RoomUsers._instance == null) {
				RoomUsers._instance = new RoomUsers();
			}
			return RoomUsers._instance;
		}
		
		private function _initialize() : void
		{
			this._user_list = { };
			this._sit_list  = { };
		}
		
		/**
		 * 添加玩家到房間用戶列表
		 * @param	r_user
		 */
		public function addUser(r_user : GameUser) : void {
			var _uid : int = r_user.id;
			var _sit : int = r_user.sit;			
			this._user_list[_uid] = r_user;
			if (_sit > 0) {
				this._sit_list[_sit] = r_user;
			}
		}
		
		/**
		 * 移除玩家
		 * @param	r_uid int 玩家ID
		 */
		public function removeUser(r_uid : int) : void
		{
			var _user : GameUser = _user_list[r_uid]; 
			var _sit : int = _user.sit;
			if (_sit > 0 ) {
				delete _sit_list[_sit];				
			}
			delete _user_list[r_uid];
		}
		
		/**
		 * 获取所有坐下的玩家
		 * @return
		 */
		public function getSitUsers() : Object
		{
			return this._sit_list;
		}
		/**
		 * 获取玩家数据对象
		 * @param	r_num int 玩家的id或者座位号
		 * @param	r_type String  uid |sit 根据玩家id或者座位号获取玩家数据对象GameUser 
		 * @return GameUser
		 */
		
		public function getUser(r_num : int, r_type:String = "uid") : GameUser
		{
			var _user : GameUser;
			if (r_type == "uid") {
				_user = _user_list[r_num];
			}else {
				_user = _sit_list[r_num];
			}
			return _user;
		}
		/**
		 * 根據玩家ID獲取玩家座位號
		 * @param	r_uid
		 * @return int
		 */
		public function getUserSit(r_uid : int) : int
		{
			var _user : GameUser = _user_list[r_uid];
			var _sit : int = _user.sit;
			return _sit;
		}
		
		/**
		 * 設置玩家座位號
		 * @param	r_uid
		 * @param	r_sit
		 */
		public function userSit(r_uid : int, r_sit:int) : void
		{
			var _user : GameUser = _user_list[r_uid];
			if(_user){				
				_sit_list[r_sit] = _user;				
				_user.sit = r_sit;
			}		
		}
		
		/**
		 * 玩家站起
		 * @param	r_uid
		 * @param	r_sit
		 */
		public function userUp(r_uid: int, r_sit:int) : void
		{
			var _user : GameUser = _user_list[r_uid];
			var _sit = _user.sit;
			//if (r_sit == _sit) {
				delete _sit_list[_sit];
				_user.sit = 0; 			
				_user.bet = 0;
				_user.clearPoker();
			//}
		}

		/**
		 * 玩家下注
		 * @param	r_uid int
		 * @param	r_bet int
		 */
		public function userBet(r_uid:int,r_bet : int) : void
		{
			var _user : GameUser = this._user_list[r_uid];
			if(_user){
				_user.chip -= r_bet; 
			}
		}
		
		/**
		 * 获取当前所有游戏玩家最大下注码
		 */
		public function userMaxBet() : int
		{
			var _user : GameUser;
			var _user_bet : int;
			var _max_bet : int;
			for each(_user in _sit_list) {
				_user_bet = _user.bet;
				_max_bet = _max_bet < _user_bet ? _user_bet : _max_bet;
			}
			return _max_bet;
		}
		
		/**
		 * 清除玩家下注数据
		 */
		public function clearUserBet() : void
		{
			var _user : GameUser;
			for each(_user in _sit_list) {
				if(_user){
					_user.bet = 0;
				}
			}
		}
		
		/**
		 * 获取当前所有游戏玩家持有的最大筹码
		 */
		public function userMaxChip() : int
		{
			var _user : GameUser;
			var _user_chip : int;
			var _max_chip: int;
			for each(_user in  _sit_list) {
				_user_chip = _user.chip;
				_max_chip = _max_chip < _user_chip ? _user_chip : _max_chip;
			}
			return _max_chip;
		}
		
		public function clearPoker() : void
		{
			var _user : GameUser;			
			for each(_user in _sit_list) {
				_user.clearPoker();
			}
		}
		
		public function destory() : void
		{
			this._user_list = {};
			this._sit_list = {};
			RoomUsers._instance = null;
		}
		
	}
	
}
